<template>
  <div>
    <CommCard title="今日订单" :count="orderToday | numberFormat">
      <template #main>
        <div class="main" ref="main"></div>
      </template>

      <template #bottom>
        <span class="leftText">昨日订单量</span>
        <span>{{ orderLastDay | numberFormat }}</span>
      </template>
    </CommCard>
  </div>
</template>

<script>
import { mapState } from "vuex";
import throttle from "lodash/throttle";
// import throttle from './lodash/debounce'
export default {
  name: "TodayOrder",
  computed: {
    //   "orderToday": 2562061,
    // "orderLastDay": 2100000,
    ...mapState({
      orderToday: state => state.echarts.echartsData.orderToday,
      orderLastDay: state => state.echarts.echartsData.orderLastDay,
      orderTrend: state => state.echarts.echartsData.orderTrend,
      orderTrendAxis: state => state.echarts.echartsData.orderTrendAxis
    })
  },
  mounted() {
    // 1.准备带宽高的容器
    // 2.初始化一个echarts实例对象
    // 3.通过echarts实例对象调用setOption方法，传递配置对象
    this.chart = this.$echarts.init(this.$refs.main);
    this.chart.setOption(this.getOptions());

    window.addEventListener(
      "resize",
      throttle(() => {
        this.chart.resize();
      }, 200)
    );
  },
  watch: {
    orderToday: {
      handler(newVal) {
        this.chart.setOption(this.getOptions());
      }
    }
  },
  methods: {
    getOptions() {
      return {
        //标题
        title: {
          text: ""
        },
        // 网格 控制整个图标在容器当中的大小
        // 图标距离容器的上下左右边距
        grid: {
          left: 0,
          right: 0,
          top: 0,
          bottom: 0
        },
        // x轴
        xAxis: {
          data: this.orderTrendAxis || [],
          show: false
          // boundaryGray: true
        },
        // y轴
        yAxis: {
          show: false
        },
        // 提示
        tooltip: {
          trigger: "axis"
          // backgroundColor: "rgba(0,0,0,.5)"
        },
        // 系列（说的是图的线line 柱bar 饼pie）
        series: {
          type: "line",
          name: "实时订单数",
          data: this.orderTrend || [],
          smooth: true, //平滑曲线
          areaStyle: {
            color: "hotpink" //区域颜色
          },
          lineStyle: {
            opacity: 0 //去掉线
          },
          itemStyle: {
            opacity: 0 //去掉折点
          }
        }
      };
    }
  }
};
</script>

<style lang="scss" scoped>
.main {
  width: 100%;
  height: 100%;
}
</style>
